package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.CapBacDao;
import com.tt.fashionWebSite.domain.CapBac;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_MemberLevelService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_memberLevelService")
public class Ad_MemberLevelServiceImp extends GenericService implements Ad_MemberLevelService {
    @Autowired
    @Qualifier("capBac")
    private CapBacDao capBacDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminMemberLevel(CapBacDto memberLevelDto) {
        CapBac capBac = capBacDao.findById(memberLevelDto.getMaCB());
        if(capBac == null){
            capBac = new CapBac();
            capBac.setMaCB(memberLevelDto.getMaCB());
            capBac.setTenCB(memberLevelDto.getTenCB());
            capBacDao.insert(capBac);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<CapBacDto> getAllLevel () {
        return mapper.mapCollection(capBacDao.getInfoRegister(), CapBacDto.class);
    }

    public CapBacDto getLevelInfoEdit(CapBacDto levelDto){
        CapBac capBac = capBacDao.findById(levelDto.getMaCB());
        CapBacDto capBacDto = mapper.map(capBac, CapBacDto.class,CAP_BAC_MAP_ID);
        return  capBacDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public CapBacDto registerLevelEdit(CapBacDto levelDto) {
        CapBac capBac = capBacDao.findById(levelDto.getMaCB());
        try {
            capBac.setMaCB(levelDto.getMaCB());
            capBac.setTenCB(levelDto.getTenCB());
            capBacDao.update(capBac);
            return levelDto;
        }
        catch (Exception ex) {
            return null;
        }
    }
}
